package com.ebt.m.customer.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.event.EventMultiChooseAll;
import com.ebt.m.customer.event.EventMultiChooseDelete;
import com.ebt.m.customer.event.EventMultiChooseIsAll;
import com.ebt.m.customer.event.EventMultiChooseOff;
import com.ebt.m.customer.event.EventMultiChooseOn;
import com.ebt.m.customer.event.EventMultiChooseUpdateCount;
import com.ebt.m.customer.event.EventMultiUnchooseAll;
import com.ebt.m.customer.util.MultiChooseManager;
import com.ebt.m.customer.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Rick.Wang on 2016/4/12.
 */
public class CustomerChooseBottomView extends RelativeLayout implements View.OnClickListener, ValueAnimator.AnimatorUpdateListener,
    ValueAnimator.AnimatorListener{

    private TextView mSelectAllView;
    private TextView mDeleteView;
    private int mHeight;
    private static final int DURATION = 320;
    private View root;

    public CustomerChooseBottomView(Context context) {
        this(context, null);
    }

    public CustomerChooseBottomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerChooseBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = inflate(context, R.layout.view_customer_multiselect_bottom, this);
        mSelectAllView = (TextView)contentView.findViewById(R.id.tv_select_all);
        mDeleteView = (TextView)contentView.findViewById(R.id.tv_delete);
        root = contentView.findViewById(R.id.root);
        mSelectAllView.setOnClickListener(this);
        mDeleteView.setOnClickListener(this);
        va = ValueAnimator.ofFloat(0,1);
        va.addUpdateListener(this);
        va.addListener(this);
        post(new Runnable() {
            @Override
            public void run() {
                mHeight = getLayoutParams().height;
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_select_all:
                if(MultiChooseManager.getInstance().checkIfAllSelected()){
                    //取消全选
                    EventBus.getDefault().post(new EventMultiUnchooseAll());
                    MultiChooseManager.getInstance().clearFlags();
                    EventBus.getDefault().post(new EventMultiChooseUpdateCount());
                }else {
                    //全选
                    EventBus.getDefault().post(new EventMultiChooseAll());
                    MultiChooseManager.getInstance().selectAll();
                    EventBus.getDefault().post(new EventMultiChooseUpdateCount());
                }
                break;
            case R.id.tv_delete:
                deleteConfirm();
                break;
        }
    }

    public void multiOn(){
        MultiChooseManager.getInstance().clearFlags();
        MultiChooseManager.getInstance().setInMultiChooseMode(true);
        EventBus.getDefault().post(new EventMultiChooseUpdateCount());
        show();
    }

    public void multiOff(){
        MultiChooseManager.getInstance().clearFlags();
        MultiChooseManager.getInstance().setInMultiChooseMode(false);
        EventBus.getDefault().post(new EventMultiChooseUpdateCount());
        hide();
    }

    @Override
    public void onAnimationStart(Animator animator) {
        switch (mState){
            case showing:
                setVisibility(View.VISIBLE);
                break;
            case shown:
                mState = State.hiding;
                break;
            case hidden:
                mState = State.showing;
                break;
            case hiding:
                break;
        }
    }

    @Override
    public void onAnimationEnd(Animator animator) {
        if (mState == State.showing){
            mState = State.shown;
        }
        if (mState == State.hiding){
            mState = State.hidden;
            setVisibility(View.GONE);
        }
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }

    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        Float f = (Float)valueAnimator.getAnimatedValue();
        if(f == null){
            return;
        }

        if (mState == State.showing){
            //正在显示
            root.setTranslationY(mHeight * (1 - f));
            return;
        }
        if (mState == State.hiding){
            //正在隐藏
            root.setTranslationY(mHeight * f);
            return;
        }
    }

    private enum State{
        showing,shown,hiding,hidden
    }
    private State mState = State.hidden;

    private ValueAnimator va;
    public void show(){
        if (getVisibility() != View.VISIBLE){
            setVisibility(View.VISIBLE);
        }

        if (va.isRunning()){
            Float f = (Float)va.getAnimatedValue();
            if (f == null){
                return;
            }
            if (mState == State.hiding){
                va.cancel();
                va.setFloatValues((1 - f), 1);
                mState = State.showing;
                va.setDuration((long)(DURATION * f));
                va.start();
            }
        }else{
            va.setFloatValues(0, 1);
            mState = State.hidden;
            va.setDuration(DURATION);
            va.start();
        }
    }

    public void hide(){
        if (va.isRunning()){
            Float f = (Float)va.getAnimatedValue();
            if (f == null){
                return;
            }
            if (mState == State.showing){
                va.cancel();
                va.setFloatValues((1 - f), 1);
                mState = State.hiding;
                va.setDuration((long)(DURATION * f));
                va.start();
            }
        }else{
            va.setFloatValues(0, 1);
            mState = State.shown;
            va.setDuration(DURATION);
            va.start();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMultiChooseOn event) {
        multiOn();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMultiChooseOff event) {
        multiOff();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMultiChooseIsAll event) {
        if (event != null){
            if(event.getSelectedAll()){
                //所有的都已经选中
                mSelectAllView.setText("取消全选");
            }else{
                //不是全部都选中
                mSelectAllView.setText("全选");
            }
        }
    }

    private ConfirmDialog mDeleteConfirmDialog;

    private void deleteConfirm() {

        if(MultiChooseManager.getInstance().getSelectedObjects().size() == 0){
            ToastUtil.showToast("没有选中客户");
            return;
        }
        // 删除确认
        mDeleteConfirmDialog = new ConfirmDialog(getContext());
        mDeleteConfirmDialog.setMessage("确定要删除吗？");
        mDeleteConfirmDialog.setPositiveButton("删除", new OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new EventMultiChooseDelete());
                mDeleteConfirmDialog.dismiss();
            }
        });
        mDeleteConfirmDialog.show();
    }
}